<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width , initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var Singleton = (function () {
            var instance
            function User(name, age) {
                this.name = name
                this.age = age
            }
            return function (name, age) {
                if (!instance) {
                    instance = new User(name, age)
                }
                return instance
            }
        })()
        // 打印出来一个user对象 「age:18,name:井」 且在单例 中只会执行一次，形成的闭包并不会被垃圾回收机制所回收
        new Singleton("井", 18)
        new Singleton("井123", 18)     //  输出单例模式的闭包 形成的「age:18,name:井」
    </script>
</body>

</html>